package com.ocean.recommendationserver.realtimerecommend.service.impl;

import com.ocean.recommendationserver.realtimerecommend.entity.Rating;
import com.ocean.recommendationserver.realtimerecommend.mapper.RatingMapper;
import com.ocean.recommendationserver.realtimerecommend.service.IRatingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ocean
 * @since 2021-07-18
 */
@Service
public class RatingServiceImpl extends ServiceImpl<RatingMapper, Rating> implements IRatingService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public Rating ratingForAMovie(Integer uid, Integer mid, Double rating) {
        Rating ratingEntity = new Rating();
        ratingEntity.setUid(uid);
        ratingEntity.setMid(mid);
        ratingEntity.setScore(rating);
        ratingEntity.setTimestamp(System.currentTimeMillis() / 1000);
        this.save(ratingEntity);

        ListOperations<String, String> opsForList = redisTemplate.opsForList();
        opsForList.leftPop("uid:" + uid);
        opsForList.rightPush("uid:" + uid, mid + ":" + rating);

        return ratingEntity;
    }
}
